package software.hust.com.hust_last.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import software.hust.com.hust_last.R;
import software.hust.com.hust_last.bean.MyPackage;
import software.hust.com.hust_last.presenter.MyPackagePresenter;
import software.hust.com.hust_last.util.LoadMoreItem;
import software.hust.com.hust_last.util.MyPackageItem;
import software.hust.com.hust_last.util.RecyclerAdapter;
import software.hust.com.hust_last.util.RecyclerItem;
import software.hust.com.hust_last.util.RecyclerLoadListener;

/**
 * @des 我的道具
 */
public class MyPackageAct extends Activity {

    @BindView(R.id.my_package_rv)
    RecyclerView mRecyclerView;
    @BindView(R.id.my_package_swipe)
    SwipeRefreshLayout mSwipeLayout;

    RecyclerAdapter<MyPackage> mAdapter;
    List<MyPackage> mData = new ArrayList<>();

    LoadMoreItem mLoadMoreItem;
    int type;
    MyPackagePresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_package);

        ButterKnife.bind(this);
        initRecyclerView();
        type = getIntent().getIntExtra("type", -1);
        mPresenter = new MyPackagePresenter(this);
        mPresenter.requestData(0);
    }

    private void initRecyclerView() {
        mSwipeLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.requestData(0);
            }
        });
        mSwipeLayout.setRefreshing(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mLoadMoreItem = new LoadMoreItem(this, mRecyclerView);
        mAdapter = new MyPackageAct.InnerAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnScrollListener(new RecyclerLoadListener() {
            @Override
            public void onLoadMore() {
                mPresenter.requestMore(mData.size());
            }
        });
    }

    public void setData(List<MyPackage> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        mAdapter.notifyDataSetChanged();
        mSwipeLayout.setRefreshing(false);
        mLoadMoreItem.setLoading(true);
    }

    public void appendData(List<MyPackage> data, boolean hasMore) {
        mData.addAll(data);
        mAdapter.notifyItemRangeInserted(mData.size() - 1, data.size());
        if (!hasMore) {
            mLoadMoreItem.setLoading(false);
        }
    }

    private class InnerAdapter extends RecyclerAdapter<MyPackage> {
        @Override
        protected List<MyPackage> getData() {
            return mData;
        }

        @Override
        public int getItemCount() {
            return getData().size();
        }

        @Override
        protected RecyclerItem<MyPackage> getItem(int viewType) {
            return new MyPackageItem(MyPackageAct.this, mRecyclerView, type);
        }

        @Override
        public void onViewAttachedToWindow(RecyclerHolder holder) {
            super.onViewAttachedToWindow(holder);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) lp;
                params.setFullSpan(holder.getItemViewType() == 1);
            }
        }
    }
}
